﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace dasBlog.Storage
{
    [DataContract(Namespace=Names.DataContractNamespace)]
    public sealed class PathMapping
    {
        [DataMember]
        public PathSegmentName PathSegment { get; set; }
        [DataMember]
        public NodeDescription Node { get; set; }
        
        public PathMapping()
        {

        }

        public PathMapping(PathSegmentName name, NodeDescription storage)
        {
            this.PathSegment = name;
            this.Node = storage;
        }
    }

    [DataContract(Namespace = Names.DataContractNamespace)]
    public sealed class PathMappings : IEnumerable<PathMapping> 
    {
        List<PathMapping> _mappings;

        public PathMappings()
        {
            _mappings = new List<PathMapping>();
        }

        public PathMappings(params PathMapping[] relationships)
        {
            _mappings = new List<PathMapping>();
            foreach (PathMapping rel in relationships)
            {
                Add(rel);
            }
        }

        
        public void Add(PathSegmentName segmentName, NodeDescription node)
        {
            PathMapping mapping = new PathMapping(segmentName, node);
            _mappings.Add(mapping);
        }

        public void Add(PathMapping rel)
        {
            _mappings.Add(rel);
        }

        public PathMapping this[PathSegmentName name]
        {
            get
            {
                 return _mappings.Find((m)=>m.PathSegment.Equals(name));
            }
        }

        [DataMember]
        public PathMapping[] Mappings
        {
            get
            {
                return _mappings.ToArray();
            }
            set
            {
                _mappings = new List<PathMapping>(value);
            }
        }

        #region IEnumerable<ChildMapping> Members

        public IEnumerator<PathMapping> GetEnumerator()
        {
            return _mappings.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _mappings.GetEnumerator();
        }

        #endregion
    }
}
